//
//  PhotoTVController.m
//  WildDonkey
//
//  Created by 佟小波 on 15/3/31.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import "PhotoTVController.h"
#import "AFNetworking.h"
#import "PhotoDetail.h"
#import "UIImageView+WebCache.h"
#import "DVSwitch.h"

@interface PhotoTVController ()

@property (nonatomic,retain) NSMutableArray *photoArray;

@end

@implementation PhotoTVController

//static NSString * const reuseIdentifier = @"Cell";


- (void) loadInfo
{
    
//    NSString *str=[NSString stringWithFormat:@"http://www.roadqu.com/api/mobile/qunawan/poi/detail&a_t=1427875763000&token=55150e044f7c3&code=%@",self.code];
    
    NSLog(@"code:%@",self.code);
    
    NSString *str=[NSString stringWithFormat:@"http://www.roadqu.com/api/mobile/qunawan/poi/detail&a_t=1427875763000&token=55150e044f7c3&code=6e133f9221c36009011e367571778101&sign=68fa74aad0147f2ee78ca1f7074327b3c5793797"];
   
//    NSLog(@"code:%@",self.code);

    AFHTTPSessionManager *mgr=[[AFHTTPSessionManager alloc]init];
    [mgr GET:str parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
        //        NSLog(@"%@",responseObject);
        
        NSDictionary *dic = [responseObject valueForKey:@"data"];
        NSArray *arr = [dic valueForKey:@"pic"];
        //        NSLog(@"%@",arr);
        self.photoArray=[NSMutableArray arrayWithCapacity:[_photoArray count]];
        
        for (NSDictionary *photoDic in arr) {
            PhotoDetail *pho=[[PhotoDetail alloc]init];
            [pho setValuesForKeysWithDictionary:photoDic];
            [self.photoArray addObject:pho];
            [pho release];
        }
        //
        //        PhotoDetail *de=self.photoArray[0];
        //
        //        NSLog(@"%@",de.url);
        
        [self.collectionView reloadData];
        
    } failure:nil];
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
    

    self.view.backgroundColor=[UIColor whiteColor];
    
    [self loadInfo];
    
    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Register cell classes
//    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    // Do any additional setup after loading the view.
    NSInteger margin = 20;
    
    self.switcher = [[DVSwitch alloc] initWithStringsArray:@[@"印象", @"相册"]];
    self.switcher.frame = CGRectMake(margin, margin * 2, self.view.frame.size.width - margin * 8, 28);
    self.switcher.selectedIndex=1;
    //    [self.view addSubview:self.switcher];
    [self.switcher setPressedHandler:^(NSUInteger index) {
     
        UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
        PhotoTVController *photo=[[PhotoTVController alloc] initWithCollectionViewLayout:layout];
        if (index==0) {
            self.switcher.selectedIndex=0;
            [self.navigationController popViewControllerAnimated:NO];
//            [self.view removeFromSuperview];
        }
        
        
    }];
    [self.navigationItem setTitleView:self.switcher];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


//定义展示的Section的个数
#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
#warning Incomplete method implementation -- Return the number of sections
    return 1;
}


//定义展示的UICollectionViewCell的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
#warning Incomplete method implementation -- Return the number of items in the section
    return [_photoArray count];
}





//每个UICollectionView展示的内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
  
    PhotoDetail *dtl=_photoArray[indexPath.row];
    
//    NSLog(@"%@",dtl.url);
    
    NSString *reuseIdentifier=@"reuse";
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];

   
    UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 20, 100, 100)];
    [imgView setImageWithURL:[NSURL URLWithString:dtl.url]];

    cell.backgroundView=imgView;
    
//    if (indexPath.row==1) {
//        cell.backgroundView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dd"]];
//    }
 
    
    self.collectionView.backgroundColor=[UIColor whiteColor];
   
    return cell;
}

#pragma mark <UICollectionViewDelegate>


//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(96, 100);
}



//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(5, 5, 5, 5);
}



//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell * cell = (UICollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
}




//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}




/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

@end
